#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include <chrono>
#include <memory>

using namespace std::chrono_literals;

class Talker : public rclcpp::Node
{
public:
    Talker() : Node("talker"), count_(0)
    {
        // Declare parameter for publish frequency with default value 10Hz (0.1s)
        this->declare_parameter<double>("publish_frequency", 10.0);
        
        // Get parameter value
        double publish_frequency = this->get_parameter("publish_frequency").as_double();
        
        // Calculate period in milliseconds
        auto period = std::chrono::milliseconds(static_cast<int>(1000.0 / publish_frequency));
        
        publisher_ = this->create_publisher<std_msgs::msg::String>("hello_topic", 10);
        timer_ = this->create_wall_timer(
            period, std::bind(&Talker::timer_callback, this));
        
        RCLCPP_INFO(this->get_logger(), "Talker node started with frequency: %.1f Hz", publish_frequency);
    }

private:
    void timer_callback()
    {
        auto message = std_msgs::msg::String();
        message.data = "Hello, ROS2: " + std::to_string(count_++);
        publisher_->publish(message);
        RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
    }

    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    size_t count_;
};

int main(int argc, char * argv[])
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<Talker>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}